#飞船类

import os
from iImageShape import ImageShape 
class Ship(ImageShape):
    '''飞船类'''
    def __init__(self,screen,rootUrl,speed:float=1.0):    
        imgUrl = os.path.join(rootUrl,"images","ship.bmp")
        
        super().__init__(screen,imgUrl)

        self.speed = speed
        self.move_up = False
        self.move_down = False
        self.move_left = False
        self.move_right = False

        #确定飞船的位置
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
    
   
    # 由于self.rect.centery等值只能储存整数 所以 当speed位小数的时候有BUG    
    def update(self):        
        '''更新位置的函数'''
        if self.move_up and self.rect.top > 0:
            self.rect.centery -= self.speed
        elif self.move_down and self.rect.bottom < self.screen_rect.bottom:            
            self.rect.centery += self.speed 
        elif self.move_left and self.rect.left > 0:
            self.rect.centerx -= self.speed
        elif self.move_right and self.rect.right < self.screen_rect.right:
            self.rect.centerx += self.speed

    def reset(self):
        self.move_up = False
        self.move_down = False
        self.move_left = False
        self.move_right = False
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
